<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>地域管理</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <link rel="stylesheet" href="/oauthplatform/static/css/x-admin/font.css">
        <link rel="stylesheet" href="/oauthplatform/static/css/x-admin/xadmin.css">
        <link rel="stylesheet" href="/oauthplatform/static/lib/zTree/css/zTreeStyle/zTreeStyle.css">
    </head>

    <body>
        <div class="x-nav">
            <span class="layui-breadcrumb">
                <a>首页</a>
                <a><cite>地域管理</cite></a>
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
            </a>
        </div>

        <div class="layui-fluid layui-row layui-col-space15">
            <div class="layui-col-sm12 layui-col-md3">
                <div class="layui-card">
                    <div class="layui-card-header">地域树</div>
                    <div class="layui-card-body">
                        <div id="treeDiv" class="ztree"></div>
                    </div>
                </div>
            </div>
            <div class="layui-col-sm12 layui-col-md9">
                <div class="layui-card">
                    <div class="layui-card-header">详细信息</div>
                    <div class="layui-card-body ">
                        <form class="layui-form layui-col-space5" id="searchForm">
                            <div class="layui-inline layui-show-xs-block">
                                <input type="text" name="name" id="name" placeholder="请输入地域名称" class="layui-input">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" type="button" onclick="reloadTable()"  title="搜索">
                                    <i class="layui-icon">&#xe615;</i>
                                </button>
                            </div>
                            <button type="button" class="layui-btn layui-btn-sm" title="添加" onclick="addArea()">
                                <i class="layui-icon">&#xe654;</i>
                            </button>
                        </form>
                    </div>

                    <div class="layui-card-body">
                        <div id="dataTables"></div>
                    </div>
                </div>
            </div>

        </div>
    </body>
    <script type="text/javascript" src="/oauthplatform/static/lib/jquery/jquery.3.4.1.js"></script>
    <script src="/oauthplatform/static/lib/layui/layui.js" charset="utf-8"></script>
    <script src="/oauthplatform/static/lib/other/html5.min.js"></script>
    <script src="/oauthplatform/static/lib/other/respond.min.js"></script>
    <script src="/oauthplatform/static/lib/zTree/js/jquery.ztree.core.js"></script>
    <script src="/oauthplatform/static/lib/x-admin/xadmin.js"></script>
    <script type="application/javascript">

        $(function(){
            loadTree();
        })

        var treeObj;
        function loadTree(){
            var setting = {
                async:{
                    enable : true,
                    url : '/oauthplatform/area/tree',
                    type : "post"
                },
                data:{
                    simpleData:{
                        enable : true,
                        idKey : "id",
                        pIdKey : "pid"
                    }
                },
                callback:{
                    beforeClick:function(treeId, treeNode, clickFlag){
                        //设置节点点击事件以此加载右侧表格数据
                        reloadTable(treeNode.id);
                    }
                }
            };
            treeObj = $.fn.zTree.init($("#treeDiv"), setting);
        }






        var table ;
        layui.use('table', function(){
            table = layui.table;
            //第一个实例
            table.render({
                elem: '#dataTables'
                ,url: '/oauthplatform/area/list' //数据接口
                ,parseData: function(res){ //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": "", //解析提示文本
                        "count": res.length, //解析数据长度
                        "data": res //解析数据列表
                    };
                }
                ,page: false //开启分页
                ,cols: [[ //表头
                    {field: 'areaId', title: '地域代码', fixed: 'left'}
                    ,{field: 'areaName', title: '名称'}
                    ,{field: 'isValid', title: '操作',templet: function(d){
                            var buttonHtml = '';
                            buttonHtml += '<button type="button" class="layui-btn layui-btn-sm" title="删除" onclick="deleteArea('+d.areaId+')">'+
                                    '<i class="layui-icon">&#xe640;</i></button>';
                            return buttonHtml;
                        }}
                ]]

            });
        });

        function deleteArea(areaId){
            if(areaId==null||areaId==''){
                layer.msg("参数异常,请联系管理员");
                return false;
            }
            $.ajax({
                type: "POST",//方法类型
                url: "/oauthplatform/area/delete" ,//url
                data: {areaId:areaId},
                success: function (result) {
                    layer.msg(result.msg);
                    if(parseInt(result.code) == 200){
                        setTimeout(function(){ location.reload(); }, 3000);
                    }
                },
                error: function (result) {
                    layer.msg("系统异常,请联系管理员");
                }
            });
        }

        function reloadTable(pid){
            table.reload('dataTables',{
                where:{name : $("#name").val(), isValid : $("#isValid").val(),pid:pid}
            });
        }


        function addArea(){

            var checkObj = treeObj.getSelectedNodes();
            if(checkObj == null || checkObj.length == 0){
                layer.msg("请先选择地域树主节点");
                return false;
            }
            var areaId = "";
            areaId = checkObj[0].id;
            if(areaId == null || areaId == ''){
                layer.msg("参数异常");
                return false;
            }

            xadmin.open('添加地域','/oauthplatform/area/add?areaId='+areaId,500,300,false);


        }

    </script>
</html>
